package com.zjt.dao.user;

import com.zjt.entity.Record;
import org.apache.commons.net.nntp.Article;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-9-7
 * Time: PM10:38
 * To change this template use File | Settings | File Templates.
 */
@Component
public class RecordDaoExpand implements IRecordDaoExpand {
    private String Query_Record_By_UserId = "";
    private String Count_Record_By_UserId = "select count(*) from Record u";
    private static Logger logger = LoggerFactory.getLogger(RecordDaoExpand.class);
    @PersistenceContext
    private EntityManager entityManager ;

    @Override
    public List<Record> getRecordListByUserId(long userId, int startRow, int pageSize) {
        //List<Record> resultList = new ArrayList<Record>();
        if (userId>0)
            Query_Record_By_UserId = "select u from Record u where u.userId = "+userId+" group by u.codeValue  order by u.updateDate desc" ;
        else
            Query_Record_By_UserId = "select u from Record u where u.userId >0 " +
                    "group by u.codeValue,u.userId order by u.updateDate desc" ;
//        Query_Record_By_UserId = "select u.id,-1 as userId,u.codeType,u.codeValue,u.goodsReal,u.productId,u.total,u.createDate,u.updateDate,u.enableFlag from Record u where u.userId >0 " +
  //              "group by u.codeValue,u.userId order by u.updateDate desc" ;

        List<Record> resultList = entityManager.createQuery(Query_Record_By_UserId).setFirstResult(startRow).setMaxResults(pageSize).getResultList();
        /*
        for (int i = 0;i<objectList.size();i++)
        {
            logger.info("object1111111111="+objectList.toString());
            try
            {
                //Record record = new Record();
                Object[] object = (Object[])objectList.get(i);
                for (int j=0;j<object.length;j++)
                {

                }
                //Record record1 = (Record)objectList.get(i);
                //logger.info("record1="+record1.toString());
                logger.info("object="+object.toString());
            }catch (Exception e){logger.error("objectList="+e);}
        }
        */
//        for (Map<String, Object> map : objectList) {
//            try
//            {
//                Record record = new Record();
//
//                logger.info("record="+record.toString());
//                resultList.add(record);
//            }catch (Exception e){}
       // }
        return resultList;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
